package com.ruoyi.web.controller.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
//import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Properties;

@RestController
public class EmailController {

    @GetMapping("/sendEmail")
    public String sendEmail(String to) {
        try {
            // 创建邮件发送器
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

            // 配置QQ SMTP服务器
            mailSender.setHost("smtp.qq.com");
            mailSender.setPort(465); // QQ SMTP SSL端口
            mailSender.setUsername("2052309069@qq.com"); // 替换为你的QQ邮箱
            mailSender.setPassword("orxtefyabmcbccbd"); // 替换为QQ邮箱授权码

            // 配置协议参数
            Properties props = mailSender.getJavaMailProperties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

            // 创建邮件内容
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true); // true 表示支持多部分消息（如附件）

            helper.setFrom("2052309069@qq.com"); // 与username一致
            helper.setTo(to);
            helper.setSubject("异常报告");
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            Date date = new Date();
// this object contains the current date value
//            System.out.println(formatter.format(date));

            helper.setText("异常报告，发送时间：" + formatter.format(date));

            // 添加附件
            FileSystemResource file = new FileSystemResource(new File("D:\\Desktop\\异常.xls"));
            helper.addAttachment(file.getFilename(), file); // 将文件作为附件添加到邮件中

            // 发送邮件
            mailSender.send(message);
            return "邮件发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "邮件发送失败：" + e.getMessage();
        }
    }
}
//@RestController
//public class EmailController {
//
//    @Value("${spring.mail.username}")
//    private String fromEmail;
//
//    @Value("${spring.mail.password}")
//    private String emailPassword;
//
//    @Value("${report.file.path}")
//    private String filePath;
//
//    @Scheduled(cron = "0 0 10 * * ?")
//    public void sendDailyReport() {
//        try {
//            JavaMailSenderImpl mailSender = createMailSender();
//            MimeMessage message = buildEmail(mailSender);
//            mailSender.send(message);
//        } catch (Exception e) {
//            // 记录错误日志
////            Logger.error("每日报告发送失败", e);
//            System.out.println("每日报告发送失败：" + e.getMessage());
//        }
//    }
//
//    private JavaMailSenderImpl createMailSender() {
//        JavaMailSenderImpl sender = new JavaMailSenderImpl();
//        sender.setHost("smtp.qq.com");
//        sender.setPort(465);
//        sender.setUsername(fromEmail);
//        sender.setPassword(emailPassword);
//
//        Properties props = new Properties();
//        props.put("mail.smtp.auth", "true");
//        props.put("mail.smtp.ssl.enable", "true");
//        sender.setJavaMailProperties(props);
//        return sender;
//    }
//
//    private MimeMessage buildEmail(JavaMailSenderImpl mailSender) throws Exception {
//        MimeMessage message = mailSender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(message, true);
//
//        helper.setFrom(fromEmail);
//        helper.setTo("xianyu4625@outlook.com");
//        helper.setSubject("每日异常报告");
//        helper.setText(buildEmailContent(), true);
//
//        File attachment = new File(filePath);
//        if (attachment.exists()) {
//            helper.addAttachment(attachment.getName(), attachment);
//        } else {
//            throw new FileNotFoundException("异常报告文件不存在");
//        }
//
//        return message;
//    }
//
//    private String buildEmailContent() {
//        return "<h1>每日系统异常报告</h1>"
//                + "<p>自动发送时间：" + LocalDateTime.now() + "</p>"
//                + "<p>本邮件包含前24小时的系统异常统计，详情请查看附件</p>";
//    }
//}